package com.ruoyi.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysSingleQuestion;
import com.ruoyi.system.domain.SysTestPaper;
import com.ruoyi.system.service.ISysTestPaperService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysStudentExam;
import com.ruoyi.system.service.ISysStudentExamService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试记录Controller
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@Controller
@RequestMapping("/system/sysStudentExam")
public class SysStudentExamController extends BaseController
{
    private String prefix = "system/sysStudentExam";

    @Autowired
    private ISysStudentExamService sysStudentExamService;

    @Autowired
    private ISysTestPaperService sysTestPaperService;

    /**
     * 教师端查看考试信息
     * @return 考试信息
     */
    @GetMapping()
    @RequiresPermissions("system:sysStudentExam:view")
    public String sysStudentExam()
    {
        return prefix + "/sysStudentExamTeacher";
    }

    /**
     * 查询考试记录列表
     */
    @RequiresPermissions("system:sysStudentExam:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStudentExam sysStudentExam)
    {
        startPage();
        List<SysStudentExam> list = sysStudentExamService.selectSysStudentExamList(sysStudentExam);
        return getDataTable(list);
    }

    /**
     * 学生端查看考试信息
     * @return 考试信息
     */
    @GetMapping(value = "/student")
    @RequiresPermissions("system:sysStudentExam:student:view")
    public String sysStudentExamStudent()
    {
        return prefix + "/sysStudentExamStudent";
    }

    /**
     * 学生端查询考试记录列表
     */
    @RequiresPermissions("system:sysStudentExam:student:list")
    @PostMapping("/studentList")
    @ResponseBody
    public TableDataInfo studentList(SysStudentExam sysStudentExam)
    {
        startPage();
        List<SysStudentExam> list = sysStudentExamService.selectSysStudentExamStudentList(sysStudentExam);
        return getDataTable(list);
    }

    /**
     * 导出考试记录列表
     */
    @RequiresPermissions("system:sysStudentExam:export")
    @Log(title = "考试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStudentExam sysStudentExam)
    {
        List<SysStudentExam> list = sysStudentExamService.selectSysStudentExamList(sysStudentExam);
        ExcelUtil<SysStudentExam> util = new ExcelUtil<SysStudentExam>(SysStudentExam.class);
        return util.exportExcel(list, "考试记录数据");
    }

    /**
     * 新增考试记录
     */
    @RequiresPermissions("system:sysStudentExam:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考试记录
     */
    @RequiresPermissions("system:sysStudentExam:add")
    @Log(title = "考试记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStudentExam sysStudentExam)
    {
        return toAjax(sysStudentExamService.insertSysStudentExam(sysStudentExam));
    }

    /**
     * 修改考试记录
     */
    @RequiresPermissions("system:sysStudentExam:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysStudentExam sysStudentExam = sysStudentExamService.selectSysStudentExamById(id);
        mmap.put("sysStudentExam", sysStudentExam);
        return prefix + "/edit";
    }

    /**
     * 修改保存考试记录
     */
    @RequiresPermissions("system:sysStudentExam:edit")
    @Log(title = "考试记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStudentExam sysStudentExam)
    {
        return toAjax(sysStudentExamService.updateSysStudentExam(sysStudentExam));
    }

    /**
     * 查询考试记录列表
     */
    @RequiresPermissions("system:sysStudentExam:exam")
    @GetMapping("/exam/{id}")
    public String exam(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysStudentExam sysStudentExam = sysStudentExamService.selectSysStudentExamById(id);
        if (Objects.equals(sysStudentExam.getStatus(), "HAVE_TAKEN_THE_EXAM")) {
            throw new RuntimeException("你已经参与考试了，请不要多次参与");
        }
        // 查询试卷信息
        SysTestPaper sysTestPaper = sysTestPaperService.selectSysTestPaperById(sysStudentExam.getTestPaperId());
        String extendedField = sysTestPaper.getExtendedField();
        List<SysSingleQuestion> sysSingleQuestions = JSON.parseArray(extendedField, SysSingleQuestion.class);
        int flag = 1;
        for (SysSingleQuestion sysSingleQuestion : sysSingleQuestions) {
            sysSingleQuestion.setSort(flag++);
        }
        sysStudentExam.setSysSingleQuestions(sysSingleQuestions);
        sysStudentExam.setStartTime(new Date());
        sysStudentExamService.updateSysStudentExam(sysStudentExam);
        mmap.put("sysStudentExam", sysStudentExam);
        return prefix + "/exam";
    }

    @RequiresPermissions("system:sysStudentExam:exam")
    @PostMapping("/examSubmit")
    @ResponseBody
    public AjaxResult examSubmit(@RequestBody SysStudentExam sysStudentExam) {
        sysStudentExamService.examSubmit(sysStudentExam);
        return toAjax(1);
    }

    /**
     * 删除考试记录
     */
    @RequiresPermissions("system:sysStudentExam:remove")
    @Log(title = "考试记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStudentExamService.deleteSysStudentExamByIds(ids));
    }

    /**
     * 证书
     */
    @RequiresPermissions("system:sysStudentExam:zhengshu")
    @GetMapping("/zhengshu/{id}")
    public String zhengshu(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysStudentExam sysStudentExam = sysStudentExamService.selectSysStudentExamById(id);
        mmap.put("sysStudentExam", sysStudentExam);
        return prefix + "/zhengshu";
    }

    /**
     * 颁发证书
     */
    @RequiresPermissions("system:sysStudentExam:zhengshu")
    @Log(title = "考试记录", businessType = BusinessType.UPDATE)
    @PostMapping("/zhengshuSave")
    @ResponseBody
    public AjaxResult zhengshuSave(SysStudentExam sysStudentExam)
    {
        sysStudentExamService.zhengshuSave(sysStudentExam);
        return toAjax(1);
    }
}
